﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace CjcPhotoPrint
{
	public class TransformableImage : Transformable
	{
		public static DependencyProperty ImageSourceProperty = ImageBrush.ImageSourceProperty.AddOwner(
			typeof( TransformableImage ),
			new PropertyMetadata( OnImageSourceChanged ) );

		private Drawing drawing;

		public TransformableImage()
		{
		}

		public TransformableImage( string url )
		{
			ImageSource = new BitmapImage( new Uri( url ) );
		}

		public ImageSource ImageSource
		{
			get { return (ImageSource)GetValue( ImageSourceProperty ); }
			set { SetValue( ImageSourceProperty, value ); }
		}

		public override string Name
		{
			get { return ImageSource.ToString(); }
		}

		public override Drawing BaseDrawing
		{
			get
			{
				if ( drawing == null )
				{
					var imageSource = ImageSource;
					if ( imageSource == null ) return null;

					drawing = new ImageDrawing(
						imageSource,
						new Rect( Transformation.RelativeSize( imageSource.Width / imageSource.Height ) ) );
				}

				return drawing;
			}
		}

		private static void OnImageSourceChanged( DependencyObject obj, DependencyPropertyChangedEventArgs args )
		{
			var transformable = obj as Transformable;
			transformable.OnPropertyChanged( "BaseDrawing" );
		}
	}
}